package helperClassPassMarkers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CreateMarker {
	
	public static void createMarker(String fileName, int xlsSheet, String packageName, String className, String myMessages, String favicon, boolean cycled) {
		
		
		File replacedFile = new File(fileName);
		replacedFile.delete();
		
		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter textFile = new BufferedWriter(fstream);
			textFile.write("package com.cyclingthealps." + packageName + ";\n");
			textFile.write("\n");
			textFile.write("import com.cyclingthealps.client.Map;\n");
			textFile.write("import com.google.gwt.core.client.GWT;\n");
			textFile.write("import com.cyclingthealps.client.PassDialog;\n");
			textFile.write("import com.google.gwt.maps.client.MapWidget;\n");
			textFile.write("import com.google.gwt.maps.client.event.MarkerClickHandler;\n");
			textFile.write("import com.google.gwt.maps.client.geom.LatLng;\n");
			textFile.write("import com.google.gwt.maps.client.overlay.Icon;\n");
			textFile.write("import com.google.gwt.maps.client.overlay.Marker;\n");
			textFile.write("import com.google.gwt.maps.client.overlay.MarkerOptions;\n");
			textFile.write("\n");
			textFile.write("public class " + className + " {\n");
			textFile.write("public Marker[] collectedPasses = new Marker[" + (count.length - 1) + "];\n");
			textFile.write("public int g = 0;\n");
			textFile.write("public " + myMessages + " res = (" + myMessages + ") GWT.create(" + myMessages + ".class);\n");
			textFile.write("public " + className + "(MapWidget map) {\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public void addPasses() {\n");
			textFile.write("for (int i = 0; i < " + (count.length - 1) + "; i++) {\n");
			textFile.write("double[][] latlngArray = latLonges();\n");
			textFile.write("LatLng point = LatLng.newInstance(latlngArray[i][0], latlngArray[i][1]);\n");
			textFile.write("String [] passName = passNames();\n");
			textFile.write("String [] siteName = siteNames();\n");
			textFile.write("String [] description = descriptions();\n");
			textFile.write("String [] route = routes();\n");
			textFile.write("String [] webelement = webelements();\n");
			textFile.write("String [] bikemap = bikemaps();\n");
			textFile.write("String [] streetview = streetviews();\n");
			textFile.write("String [] motorbikeVideo = motorbikevideos();\n");
			textFile.write("int [] d = ds();\n");
			textFile.write("String [] climbbybike = climbbybikes();\n");
			textFile.write("String [] quaeldich = quaeldichs();\n");
			textFile.write("String[] video = videos();\n");
			textFile.write("String[] pictures = pictures();\n");
			textFile.write("String[] cyclingChallenge = cyclingChallenges();\n");
			textFile.write("String[] tourReverse = tourReverses();\n");
			textFile.write("String[] andras = andrass();\n");
			textFile.write("String[] threesixty = threesixtys();\n");
			textFile.write("Map.getMap().addOverlay(createMarker(point, passName[i], siteName[i], description[i], route[i], video[i], pictures[i], webelement[i], bikemap[i], streetview[i], motorbikeVideo[i], d[i], climbbybike[i], quaeldich[i], cyclingChallenge[i],tourReverse[i],andras[i],threesixty[i]));\n");	
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public void removePasses() {\n");
			textFile.write("for (int i = 0; i < " + (count.length - 1) + "; i++) {\n");
			textFile.write("Map.getMap().removeOverlay(collectedPasses[i]);\n");
			textFile.write("g--;\n");
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");

			textFile.write("private Marker createMarker(final LatLng point, final String title, final String siteName, final String description, final String route, final String video, final String picture, final String webelement, final String bikemap, final String streetview, final String motorbikeVideo, final int d, final String climbbybike, final String quaeldich, final String cyclingChallenge, final String tourReverse, final String andras, final String threesixty) {\n");
			textFile.write("MarkerOptions options = MarkerOptions.newInstance();\n");
			textFile.write("Icon icon = Icon.newInstance();\n");
			textFile.write("icon.setImageURL(\"" + favicon + "\");\n");
			textFile.write("options.setIcon(icon);\n");
			textFile.write("options.setTitle(title + \", \" + description);\n");
			textFile.write("final Marker marker = new Marker(point, options);\n");
			textFile.write("collectedPasses[g] = marker;\n");
			
			textFile.write("g++;\n");
			textFile.write("marker.addMarkerClickHandler(new MarkerClickHandler() {\n");

			textFile.write("public void onClick(MarkerClickEvent event) {\n");
			textFile.write("Map.Zoomin(point);\n");
			textFile.write("	PassDialog.setOptions(point, title, siteName, description, route, video, picture, webelement, bikemap, streetview, motorbikeVideo, d, climbbybike, quaeldich, cyclingChallenge, tourReverse, andras, threesixty);\n");
			textFile.write("}\n");
			textFile.write("});\n");
			textFile.write("return marker;\n");
			textFile.write("}\n");
			textFile.write("\n");

			textFile.write("public double[][] latLonges() {\n");
			textFile.write("double[][] latlngArray = new double[" + (count.length - 1) + "][" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell NameCell = sheet.getCell(4, i);
				String name = NameCell.getContents();
				Cell xCell = sheet.getCell(1, i);
				String latitude = xCell.getContents();
				Cell yCell = sheet.getCell(2, i);
				String longitude = yCell.getContents();
				System.out.println(name);
				textFile.write("//" + name + "\n");
				textFile.write("latlngArray[" + (i - 1) + "][0] = " + latitude + ";\n");
				textFile.write("latlngArray[" + (i - 1) + "][1] = " + longitude + ";\n");
			}
			textFile.write("return latlngArray;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] passNames() {\n");
			textFile.write("String [] passName = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell mountainNameCell = sheet.getCell(3, i);
				String passName = mountainNameCell.getContents();
				textFile.write("passName[" + (i - 1) + "] = res." + passName + "();\n");
			}
			textFile.write("return passName;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] siteNames() {\n");
			textFile.write("String [] siteName = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(9, i);
				String siteName = siteNameCell.getContents();
				textFile.write("siteName[" + (i - 1) + "] = \"" + siteName + "\";\n");
			}
			textFile.write("return siteName;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] descriptions() {\n");
			textFile.write("String [] description = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(10, i);
				String passDescription = siteNameCell.getContents();
				textFile.write("description[" + (i - 1) + "] = res." + passDescription + "();\n");
			}
			textFile.write("return description;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] routes() {\n");
			textFile.write("String [] route = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(16, i);
				String route = siteNameCell.getContents();
				textFile.write("route[" + (i - 1) + "] = \"" + route + "\";\n");
			}
			textFile.write("return route;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] webelements() {\n");
			textFile.write("String [] webelement = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(17, i);
				String webelement = siteNameCell.getContents();
				textFile.write("webelement[" + (i - 1) + "] = res." + webelement + "();\n");
			}
			textFile.write("return webelement;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] bikemaps() {\n");
			textFile.write("String [] bikemap = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(23, i);
				String bikemap = siteNameCell.getContents();
				textFile.write("bikemap[" + (i - 1) + "] = \"" + bikemap + "\";\n");
			}
			textFile.write("return bikemap;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] streetviews() {\n");
			textFile.write("String [] streetview = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(46, i);
				String streetview = siteNameCell.getContents();
				textFile.write("streetview[" + (i - 1) + "] = \"" + streetview + "\";\n");
			}
			textFile.write("return streetview;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] motorbikevideos() {\n");
			textFile.write("String [] motorbikeVideo = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(25, i);
				String motorbikeVideo = siteNameCell.getContents();
				textFile.write("motorbikeVideo[" + (i - 1) + "] = \"" + motorbikeVideo + "\";\n");
			}
			textFile.write("return motorbikeVideo;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public int[] ds() {\n");
			textFile.write("int [] d = new int[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(26, i);
				String d = siteNameCell.getContents();
				textFile.write("d[" + (i - 1) + "] = " + d + ";\n");
			}
			textFile.write("return d;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] climbbybikes() {\n");
			textFile.write("String [] climbbybike = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(27, i);
				String climbbybike = siteNameCell.getContents();
				textFile.write("climbbybike[" + (i - 1) + "] = \"" + climbbybike + "\";\n");
			}
			textFile.write("return climbbybike;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] quaeldichs() {\n");
			textFile.write("String [] quaeldich = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(28, i);
				String quaeldich = siteNameCell.getContents();
				textFile.write("quaeldich[" + (i - 1) + "] = \"" + quaeldich + "\";\n");
			}
			textFile.write("return quaeldich;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] videos() {\n");
			textFile.write("String [] video = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell videoCell = sheet.getCell(29, i);
				String video = videoCell.getContents();
				textFile.write("video[" + (i - 1) + "] = \"" + video + "\";\n");
			}
			textFile.write("return video;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] pictures() {\n");
			textFile.write("String [] picture = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell pictureCell = sheet.getCell(30, i);
				String picture = pictureCell.getContents();
				textFile.write("picture[" + (i - 1) + "] = \"" + picture + "\";\n");
			}
			textFile.write("return picture;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] cyclingChallenges() {\n");
			textFile.write("String [] cyclingChallenge = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell cyclingChallengeCell = sheet.getCell(31, i);
				String cyclingChallenge = cyclingChallengeCell.getContents();
				textFile.write("cyclingChallenge[" + (i - 1) + "] = \"" + cyclingChallenge + "\";\n");
			}
			textFile.write("return cyclingChallenge;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] tourReverses() {\n");
			textFile.write("String [] tourReverse = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell tourReverseCell = sheet.getCell(48, i);
				String tourReverse = tourReverseCell.getContents();
				textFile.write("tourReverse[" + (i - 1) + "] = \"" + tourReverse + "\";\n");
			}
			textFile.write("return tourReverse;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] andrass() {\n");
			textFile.write("String [] andras = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell andrasCell = sheet.getCell(49, i);
				String andrasLink = andrasCell.getContents();
				textFile.write("andras[" + (i - 1) + "] = \"" + andrasLink + "\";\n");
			}
			textFile.write("return andras;\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.write("public String[] threesixtys() {\n");
			textFile.write("String [] threesixty = new String[" + (count.length - 1) + "];\n");
			for (int i = 1; i < count.length; i++) {
				Cell threesixtyCell = sheet.getCell(51, i);
				String threesixtyLink = threesixtyCell.getContents();
				textFile.write("threesixty[" + (i - 1) + "] = \"" + threesixtyLink + "\";\n");
			}
			textFile.write("return threesixty;\n");
			
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");
			

			textFile.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
